<?php
class BrandAction extends HomeCommonAction {
    public function add() {
        $id = $_POST['id'];
        if(intval($id)<=0) {return false;}
        $Share = D('Share');
        $share_info = $Share->where("id = '{$id}'")->find();
        if(count($share_info)<=0) {return false;}
        $Brand = D('Brand');
        $userid = Session::getLocal('id');
        $brand_info = $Brand->where("share_id = '{$id}' and userid = {$userid}")->find();
        if(count($brand_info)>0) {
            echo 3;return false;
        }

        $data = array();
        $data['caption'] = $_POST['caption'];
        $data['is_anonymous'] = 1;
        if(Session::getLocal('user') == 1) {
            $data['userid'] = $userid;
            $data['is_anonymous'] = 0;
        }
        $data['share_id'] = $id;
        $data['commodity_id'] = $share_info['commodity_id'];
        $data['time_add'] = time();

        if($Brand->add($data)) {
            $BaseCommodity = D('BaseCommodity');
            $BaseCommodity->setInc('brand_num',"id = '{$share_info['commodity_id']}'",1);
            echo 1;
        }
        else {
            echo 0;
        }
        return true;
    }

    public function addNum() {
        $id = $_POST['id'];
        if(intval($id)<=0) {return false;}
        $Share = D('Share');
        $share_info = $Share->where("id = '{$id}'")->find();
        if(count($share_info)<=0) {return false;}
        $userid = Session::getLocal('id');
        $Brand = D('Brand');
        $brand_info = $Brand->where("share_id = '{$id}' and userid = {$userid}")->find();
        if(count($brand_info)>0) {
            echo 3;return false;
        }
        $data = array();
        $data['is_anonymous'] = 1;
        if(Session::getLocal('user') == 1) {
            $data['userid'] = $userid;
            $data['is_anonymous'] = 0;
        }
        $data['share_id'] = $id;
        $data['commodity_id'] = $share_info['commodity_id'];
        $data['time_add'] = time();
//        $Brand->add($data);


        $caption = $_POST['caption'];

        $brand_info  = $Brand->where("caption = '{$caption}'")->find();
        if(count($brand_info)<=0) {return false;}
        $rs = $Brand->setInc('num',"id = '{$brand_info['id']}'",1);
        if($rs) {
            echo 1;
        }
        else {
            echo 0;
        }
    }
}
?>
